using Gee;

namespace Atlantis
{
    public class CompositeNode : Node
    {
        private ArrayList<Node> children;

        public CompositeNode() {
            
        }

        public ArrayList<Node> Children {
            get {
                if (children == null) {
                    children = CreateChildren();
                }

                foreach (Node child in this.children) {
                    child.Parent = this;
                }
            
                return children;
            }
        }
        
        public Node find_by_id(string id) {	
			if (Id == id){
				return this;
			}
			
			foreach(Node node in Children){
				if (node.Id == id)
					return node;
					
				if (node is CompositeNode){
					Node res = ((CompositeNode)node).find_by_id(id);
					if (res != null) {
						return res;
					}
				}
			}
			
			return null;
		}
        
        public override void on_accept(INodeVisitor visitor) {
			foreach(Node node in Children){
				node.accept(visitor);
			}
		}

        protected virtual ArrayList<Node> CreateChildren()
        {
            return new ArrayList<Node>();
        }
    }
}
